#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from libuserchan import *
from util import ereg           
                
def _ping_parser(pyrc,group):
    pingmsg = group[0]
    pyrc._pong(pingmsg)

def _raw_parser(pyrc,group):
    nil, rawcmd, botnick, message = group
    pyrc.me = botnick
    message = message.strip()
    try:
        r = int(rawcmd)
    except:
        r = -1
        
    if   r == 353:
        arr = message[2:].replace(":","").split(" ")
        chan = arr[0]
        users = arr[1:]
        for u in users:
            m = ''
            if chan in pyrc.chan:
                if u[0] in usermodes:
                    m = u[0]
                    u = u[1:]
                pyrc.getchan(chan).addUser(User(u),m)

def _privmsg_parser(pyrc,group):
    eventstr = "PRIVMSG"
    pyrc._log("%s %s"%(eventstr,str(group)))
    umask, mitt, user, host, dest, nil, nil, message = group
    message = message.strip()
    if dest == pyrc.me:
        dest = mitt
    for e in pyrc.events:
        if eventstr == e[0]:
            eventstr, e_umask,e_dest,e_mesg, e_func = e
            ris_umask = ereg(e_umask,umask)
            ris_dest = ereg(e_dest,dest)
            ris_mesg = ereg(e_mesg,message)
            if ris_umask is not None and ris_dest is not None and ris_mesg is not None:
                try:
                    e_func(pyrc,ris_umask,ris_dest,ris_mesg)
                except Exception, (errstr): 
                    print "Module Error '%s':\n\t%s" %(e_func.__module__,errstr)
    
def _notice_parser(pyrc,group):
    eventstr = "NOTICE"
    pyrc._log("%s %s"%(eventstr,str(group)))
    umask, mitt, user, host, dest, nil, nil, message = group
    message = message.strip()
    for e in pyrc.events:
        if eventstr == e[0]:
            eventstr, e_umask,e_dest,e_mesg, e_func = e
            ris_umask = ereg(e_umask,umask)
            ris_dest = ereg(e_dest,dest)
            ris_mesg = ereg(e_mesg,message)
            if ris_umask is not None and ris_dest is not None and ris_mesg is not None:
                try:
                    e_func(pyrc,ris_umask,ris_dest,ris_mesg)
                except Exception, (errstr): 
                    print "Module Error '%s':\n\t%s" %(e_func.__module__,errstr)

def _join_parser(pyrc,group):
    eventstr = "JOIN"
    pyrc._log("%s %s"%(eventstr,str(group)))
    umask, nick, user, host, chan, other,nil,nil = group
    chan = chan.strip()
    other = other.strip()
    
    pyrc.getchan(chan).addUser(User(nick),'')
    
    for e in pyrc.events:
        if eventstr == e[0]:
            eventstr, e_umask, e_chan, e_func = e
            ris_umask = ereg(e_umask,umask)
            ris_chan = ereg(e_chan,chan)
            if ris_umask is not None and ris_chan is not None:
                try:
                    e_func(pyrc,ris_umask,ris_chan)
                except Exception, (errstr): 
                    print "Module Error '%s':\n\t%s" %(e_func.__module__,errstr)
    
def _part_parser(pyrc,group):
    eventstr = "PART"
    pyrc._log("%s %s"%(eventstr,str(group)))
    umask, nick, user, host, chan, message = group
    message = message.strip()
    
    if nick != pyrc.me:
        pyrc.getchan(chan).delUser(User(nick))
    
    for e in pyrc.events:
        if eventstr == e[0]:
            eventstr, e_umask,e_chan,e_mesg, e_func = e
            ris_umask = ereg(e_umask,umask)
            ris_chan = ereg(e_chan,chan)
            ris_mesg = ereg(e_mesg,message)
            if ris_umask is not None and ris_chan is not None and ris_mesg is not None:
                try:
                    e_func(pyrc,ris_umask,ris_chan,ris_mesg)
                except Exception, (errstr): 
                    print "Module Error '%s':\n\t%s" %(e_func.__module__,errstr)
    
def _quit_parser(pyrc,group):
    eventstr = "QUIT"
    pyrc._log("%s %s"%(eventstr,str(group)))
    umask, nick, user, host, message = group
    message = message.strip()
    
    for c in pyrc.chan:
        c.delUser(nick)
    
    for e in pyrc.events:
        if eventstr == e[0]:
            eventstr, e_umask, e_mesg, e_func = e
            ris_umask = ereg(e_umask,umask)
            ris_mesg = ereg(e_mesg,message)
            if ris_umask is not None and ris_mesg is not None:
                try:
                    e_func(pyrc,ris_umask,ris_mesg)
                except Exception, (errstr): 
                    print "Module Error '%s':\n\t%s" %(e_func.__module__,errstr)
    
def _mode_parser(pyrc,group):
    pyrc._log("MODE: "+str(group))
    
def _nick_parser(pyrc,group):
    eventstr = "PRIVMSG"
    pyrc._log("%s %s"%(eventstr,str(group)))
    umask, oldnick, user, host, newnick = group

    for c in pyrc.chan:
        c.changeUserNick(oldnick,newnick)
    
    for e in pyrc.events:
        if eventstr == e[0]:
            eventstr, e_umask,e_onick,e_nnick, e_func = e
            ris_umask = ereg(e_umask,umask)
            ris_onick = ereg(e_onick,oldnick)
            ris_nnick = ereg(e_nnick,newnick)
            if ris_umask is not None and ris_onick is not None and ris_nnick is not None:
                try:
                    e_func(pyrc,ris_umask,ris_onick,ris_nnick)
                except Exception, (errstr): 
                    print "Module Error '%s':\n\t%s" %(e_func.__module__,errstr)

def _err_parser(pyrc,group):
    pyrc.closed = True